<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2018/11/20
 * Time: 11:40
 */

namespace app\models;

use yii\db\ActiveRecord;

class Party_party extends ActiveRecord {
    //获取派对列表
    public function getPartyList($limit, $offset, $cityCode, $startTime, $endTime, $partyTheme, $partyType, $partyUser, $orderBy) {
        $post = Party_party::find()->joinWith(['user']);
                

        if ($cityCode != -1){
            $post->andWhere(['party_party.city_code' => $cityCode]);
        }

        if ($startTime != -1){
            $post->andWhere(['between', 'party_party.gmt_create', $startTime, $endTime]);
        }

        if ($partyTheme != -1){
            $post->andWhere(['or', ['like', 'user_user.user_nickname', '%'.$partyTheme.'%', false], ['like','party_party.party_theme', '%'.$partyTheme.'%', false]]);
        }

        if ($partyType != -1){
            $post->andWhere(['party_party.is_deleted' => $partyType]);
        }

        if ($partyUser != -1) {
            if ($partyUser == 1) {
                $post->andWhere(['like', 'user_user.mobile', '168168']);
            } elseif($partyUser == 0) {
                $post->andWhere(['not like', 'user_user.mobile', '168168']);
            }
        }

        $count = $post->count();
        $result = $post->orderBy('party_party.party_id DESC')->limit($limit)->offset($offset)->asArray()->all();
        // return $offset;die;
        $sql = $post->createCommand()->getRawSql();
        // return $sql;die;
        return ['total'=>$count, 'rows'=>$result];
    }

    //关联用户表 获取用户信息
    public function getUser()
    {
        //同样第一个参数指定关联的子表模型类名
        //第二个参数第一个是字表的 第二个是主表的
        return $this->hasOne(User_user::className(), ['user_id' => 'user_id']);
    }
}